<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Type extends Controller_Admin_Based
{

    public function action_add()
    {
        $pagination_arr = Paginationhelp::get_data('type', 'id_type');
        $data = array(
            'result' => $pagination_arr['data'],
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/type_add', $data);
        $this->response->body($view);
    }

    public function action_do_add()
    {


        if ($_POST)
        {
            $_POST['addtime'] = time();
            $_POST['updatetime'] = time();
            $type = Type::instances()->set($_POST);
            if ($type)
            {
                Message::set('游戏类型添加成功！');
                Request::current()->redirect('/admin/type/add');
            }
        }
    }

    public function action_edit()
    {
        $id_type = $this->request->param('id');




        if ($_POST)
        {
            $_POST['updatetime'] = time();
            $type = Type::instances($id_type)->update($_POST);
            if ($type)
            {
                Message::set('游戏类型编辑成功！');
                Request::current()->redirect('/admin/type/edit/' . $id_type);
            }
        }
        $type = Type::instances($id_type)->get();
        $pagination_arr = Paginationhelp::get_data('type', 'id_type');
        $data = array(
            'result' => $pagination_arr['data'],
            'data' => $type,
            'page_view' => $pagination_arr['pagination']
        );



        $view = Viewer::display('admin/type_edit', $data);
        $this->response->body($view);
    }

    public function action_delete()
    {
        $id_type = $this->request->param('id');
        $flag = Type::instances($id_type)->delete();
        if ($flag)
        {
            Message::set('游戏类型删除成功');
            Request::current()->redirect('/admin/type/add');
        }
        else
        {
            Message::set('游戏类型删除失败', 'error');
            Request::current()->redirect('admin/type/add');
        }
    }

}
